#include "HttpSchemeFactory.h"
#include "include/wrapper/cef_helpers.h"
#include "include/wrapper/cef_stream_resource_handler.h"
#include <fstream>
#include <filesystem>
#include <iostream>

CefRefPtr<CefResourceHandler> HttpSchemeFactory::Create(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, const CefString& scheme_name, CefRefPtr<CefRequest> request)
{
    CEF_REQUIRE_IO_THREAD();
	std::string url = request->GetURL().ToString();
	std::string urlPrefix = "my://test/";
	url.erase(0, urlPrefix.length());
	size_t paramIndex = url.find_first_of('?');
	if (paramIndex != std::string::npos)
	{
		url.erase(paramIndex);
	}
	TCHAR buffer[MAX_PATH] = {0};
	GetModuleFileName(NULL, buffer, MAX_PATH);
	std::filesystem::path targetPath(buffer);
	targetPath = targetPath.parent_path().append("HTML").append(url);
	if (!std::filesystem::exists(targetPath))
	{
		DLOG(INFO) << L"try to load: " << targetPath.generic_wstring() << ", not found";
	}
	std::string ext = targetPath.extension().generic_string();
	std::string mimeType = ext == ".html" ? "text/html" : "*";
	auto stream = CefStreamReader::CreateForFile(targetPath.generic_wstring());

    return new CefStreamResourceHandler(mimeType, stream);
}
